package com.blog.admin.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.admin.bean.User;
import com.blog.admin.service.RoleService;
import com.blog.admin.service.UserService;
import com.blog.base.controller.BaseController;
import com.blog.util.MD5Util;


/**
 * @todo UserController
 * @author fg
 * @date 2016年1月19日
 */
@Scope("prototype")
@Controller
@RequestMapping(value="/user")
public class UserController extends BaseController
{ 
	@Resource
	UserService userService;
	@Resource
	RoleService roleService;

	/**
	 * @todo 查询所有用户信息
	 * @author fg
	 * @date 2016年1月25日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping(value="getUserList")
	public ModelAndView getUserList(@RequestParam("userName")String userName)
	{
		
		User user = new User();
		user.setUserName(userName);
		List<User> userList = userService.queryUserByOption(user);
		for(User userRes:userList)
		{
			userRes.setRoleList(roleService.queryRoleByUserId(userRes.getId()));
		}
		
		mav.setViewName("admin/user/user_list");
		mav.addObject("userList",userList);
		//returnPage(request,userList);
		return mav;
	}
	
	/**
	 * @todo 添加用户
	 * @author fg
	 * @date 2016年1月25日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping(value="addUser")
	@ResponseBody
	public JSONObject addUser(HttpServletRequest request,User user)
	{
		try
		{
			String[] roleId = request.getParameterValues("role");
			user.setPassword(MD5Util.MD5(DEFULT_PASSWORD));
			success = userService.addUser(user,roleId);
			if(success==1)
			{
				msg="添加成功！初始密码为："+DEFULT_PASSWORD;
			}else if(success==0)
			{
				msg="添加失败，该用户名已经存在！";
			}
			
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		return json;
	}
	
	/**
	 * @todo 打开添加用户的div
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("preAddUser")
	public ModelAndView preAddUser()
	{
		mav.setViewName("admin/user/user_add");
		mav.addObject("roleList", roleService.queryAllRole());
		return mav;
	}
	
	/**
	 * @todo 打开编辑用户的div
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("preEditUser")
	public ModelAndView preEditUser(@RequestParam("userId")String userId)
	{
		User user = new User();
		user.setId(Integer.parseInt(userId));
		user = userService.queryUserByOption(user).get(0);
		user.setRoleList(roleService.queryRoleByUserId(user.getId()));
		
		mav.setViewName("admin/user/user_edit");
		mav.addObject("user",user);
		mav.addObject("roleList", roleService.queryAllRole());
		mav.addObject("userHasRoleList", user.getRoleList());
		return mav;
	}
	
	
	/**
	 * @todo 修改用户
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping(value="editUser")
	@ResponseBody
	public JSONObject editUser(HttpServletRequest request,User user)
	{
		try
		{
			String[] roleId = request.getParameterValues("role");
			success = userService.modifyUser(user,roleId);
			msg="修改成功！";
			
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		return json;
	}
	
	
	/**
	 * @todo 删除用户
	 * @author fg
	 * @date 2016年1月30日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping(value="removeUser")
	@ResponseBody
	public JSONObject removeUser(@RequestParam("userId")String userId)
	{
		try
		{
			success = userService.removeUserByUserId(userId);
			msg = "删除成功！";
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			msg = e.toString();
		}
		finally
		{
			json.put("success", success);
			json.put("msg", msg);
		}
		return json;
	}
	
	
}
